cmake_minimum_required(VERSION 3.15) # 确保支持FetchContent
project(sql VERSION 0.1.0 LANGUAGES C CXX)

include(config.cmake)

set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

# 设置wxWidgets的配置选项
set(wxWidgets_CONFIG_OPTIONS --disable-shared --enable-monolithic --with-opengl=no --without-media --disable-richtext --disable-html --disable-gfx --disable-stl --disable-samples --disable-docs)

# 克隆并构建wxWidgets库
include(FetchContent)
FetchContent_Declare(
  wxWidgets
  GIT_REPOSITORY "https://github.com/wxWidgets/wxWidgets.git"
  GIT_TAG v3.2.2.1
  CMAKE_ARGS ${wxWidgets_CONFIG_OPTIONS}
)

FetchContent_MakeAvailable(wxWidgets)

# 包含MySQL的头文件目录和库目录
set(MYSQL_INCLUDE_DIR "${MYSQL_DIR}/include")
set(MYSQL_LIB_DIR "${MYSQL_DIR}/lib")
set(LIB_DIR "./libs")

# 包含目录设置
include_directories(${MYSQL_INCLUDE_DIR})
include_directories(${wxWidgets_SOURCE_DIR}/include)
include_directories(${LIB_DIR})

# 链接目录设置
link_directories(${MYSQL_LIB_DIR})
link_directories(${LIB_DIR})

# 指定源文件和可执行文件
set(SOURCE_FILES 
    src/main.cpp 
    src/config.cpp 
    src/db.cpp 
    src/db_op.cpp 
    src/lib_init.cpp 
    src/lib_manager.cpp 
    src/time.cpp
    src/gui.cpp

    src/Dialogs/AddBookDialog.cpp
    src/Dialogs/AddPurchaseDialog.cpp
    src/Dialogs/AddDiscardDialog.cpp
    src/Dialogs/ReturnBookDialog.cpp
)

# [tinyxml2] 引入其它源码文件
set(OTHER_FILES 
    ${LIB_DIR}/tinyxml2/tinyxml2.cpp
)
# ----------------------------------------------------------------

add_executable(sql ${SOURCE_FILES} ${OTHER_FILES})

# ----------------------------------------------------------------

# [MYSQL] 链接MySQL库
target_link_libraries(sql PRIVATE libmysql)

# [wxWidgets] 只链接wxWidgets GUI相关模块
target_link_libraries(sql PRIVATE wx::core wx::base)

# ----------------------------------------------------------------

# [MYSQL] 复制DLL文件到输出目录
add_custom_command(TARGET sql POST_BUILD
    COMMAND ${CMAKE_COMMAND} -E copy_if_different
    ${MYSQL_LIB_DIR}/libmysql.dll
    $<TARGET_FILE_DIR:sql>
)

# 复制XML文件到输出目录
add_custom_command(TARGET sql POST_BUILD
    COMMAND ${CMAKE_COMMAND} -E copy_if_different
    ${CMAKE_CURRENT_SOURCE_DIR}/config.xml
    $<TARGET_FILE_DIR:sql>
)

# [wxWidgets] 在构建完成后，将依赖的动态链接库文件拷贝到可执行文件输出目录下
add_custom_command(TARGET sql
                   POST_BUILD COMMAND "${CMAKE_COMMAND}" -E
                   copy_if_different "$<TARGET_RUNTIME_DLLS:sql>" "$<TARGET_FILE_DIR:sql>"
                   VERBATIM COMMAND_EXPAND_LISTS)


# ----------------------------------------------------------------


# [wxWidgets] Windows下，需添加以下属性，否则编译时会出现main函数未定义的错误
if(WIN32)
    set_target_properties(sql PROPERTIES WIN32_EXECUTABLE ON)
endif()

# debug模式
add_definitions(-DDEBUG_PRINTS)